<template>
	<div class="page page-login">
		<div class="box-wrapper">
			<div class="box-main-wrapper">
				<div class="box box-login">
					<h2>设置新密码</h2>
					<el-form class="login-form" ref="form" :model="form" :rules="rules">
						<el-form-item prop="password">
							<div class="icon-wrapper lock">
								<svg-icon icon-class="lock"/>
							</div>
							<el-input :type="passwordType" v-model="form.password" autoComplete="off" placeholder="新密码" />
              <div class="icon-wrapper eyes last" @click="showPwd"  :class="{ active: passwordType !== 'password' }">
                <svg-icon icon-class="eye"/>
              </div>
						</el-form-item>
						<el-form-item prop="rePassword">
							<div class="icon-wrapper lock">
								<svg-icon icon-class="lock"/>
							</div>
							<el-input :type="passwordType" v-model="form.rePassword" autoComplete="off" placeholder="再次输入新密码" />
						</el-form-item>
						<el-button class="btn-login" :loading="loading" type="primary" @click="submitForm('form')">重置密码</el-button>
						<p class="tips"><router-link to="/login">返回</router-link></p>
					</el-form>
				</div>
			</div>
		</div>
		<footer-simple />
	</div>
</template>
<script>
  import FooterSimple from '@/components/FooterSimple';

  export default {
    name: 'resetPassword',
    components: { FooterSimple },
    data() {
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.form.rePassword !== '') {
            this.$refs.form.validateField('rePassword');
          }
          callback();
        }
      };
      const validateCheckPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        loading: false,
        passwordType: "password",
        form: {
          password: '',
          rePassword: ''
        },
        rules: {
          password: [
            { trigger: 'blur', validator: validatePass}
          ],
          rePassword: [
            { trigger: 'blur', validator: validateCheckPass}
          ]
        }
      };
    },
    methods: {
      showPwd() {
        if (this.passwordType === "password") {
          this.passwordType = "text";
        } else {
          this.passwordType = "password";
        }
        this.$logger('触发密码可见度。');
      },
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.loading = true;
            /*this.$store
            .dispatch('LoginByUsername', this.form)
            .then(() => {
              console.log('route');
              this.loading = false;
              this.$router.push({ path: '/' });
            })
            .catch(() => {
              this.loading = false;
            });*/
            setTimeout(()=> {
              this.loading = false;
            }, 3000);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  };
</script>
